package com.project.visuallinks.client;

import java.io.Console;
import java.util.ArrayList;

import pl.balon.gwt.diagrams.client.connection.Connection;
import pl.balon.gwt.diagrams.client.connection.RectilinearTwoEndedConnection;
import pl.balon.gwt.diagrams.client.connection.StraightTwoEndedConnection;
import pl.balon.gwt.diagrams.client.connector.Connector;
import pl.balon.gwt.diagrams.client.connector.UIObjectConnector;

import com.allen_sauer.gwt.dnd.client.DragContext;
import com.allen_sauer.gwt.dnd.client.PickupDragController;
import com.allen_sauer.gwt.dnd.client.drop.BoundaryDropController;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.DisclosurePanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/* Singleton class
 * Class that creates all the GUI of the application. It is called once at startup and 
 * receives all input from the user
 */
public class FrontEnd {
	// interface declaration
	FlowPanel bannerPanel = new FlowPanel(); //the panel which contains the banner
	AbsolutePanel canvasPanel = new AbsolutePanel(); //the panel which contains the canvas 
	FlowPanel rightFramePanel = new FlowPanel(); //the panel with all the right bar
	FlowPanel titleOptionsPanel = new FlowPanel(); //the panel with the title of the options
	FlowPanel optionsPanel = new FlowPanel(); //the panel with the buttons, the search field
	FlowPanel optionsPanelFirstLine = new FlowPanel(); //the panel with the search fields
	HorizontalPanel optionsPanelSecondLine = new HorizontalPanel(); //the panel with the buttons
	FlowPanel titlePathPanel = new FlowPanel(); //the panel with the title of the path
	HorizontalPanel pathPanel = new HorizontalPanel(); //the panel with the path
	ScrollPanel scrollPathPanel = new ScrollPanel(); //a scrollbar for the path
	FlowPanel titleBookmarksPanel = new FlowPanel(); //a panel with a title for the bookmarks
	VerticalPanel bookmarksPanel = new VerticalPanel(); //the panel with the bookmarks
	ScrollPanel bookmarksScroll = new ScrollPanel(); //a scrollbar for the bookmarks
	FlowPanel bodyPanel = new FlowPanel(); //the panel with the canvas, the banner and the right bar
	FlowPanel allPanel = new FlowPanel(); //a panel which contains all, at the top level
	// DecoratorPanel allPanel = new DecoratorPanel();
	Image banner = new Image(); //the image banner
	TextBox accountTextBox = new TextBox(); //the textbox for searching an account
	Button okButton = new Button("Display"); //the button to launch the mapping
	Button infoContactButton = new Button("Info"); //the button to display a contact info
	Button clearButton = new Button("Clear"); //a button to clear the mapping
	Button helpButton = new Button("Help"); //a button to display the help
	ToggleButton smallTagsButton = new ToggleButton("Small"); //a button to display only the small tags
	ToggleButton mediumTagsButton = new ToggleButton("Medium"); //a button to display the medium tags
	ToggleButton largeTagsButton = new ToggleButton("Large"); //a button to display the large tags
	ToggleButton allTagsButton = new ToggleButton("A"); //a button to display all the tags
	ScrollPanel scrollPopup = new ScrollPanel(); //a scroll for a help popup
	PopupPanel popupHelp = new PopupPanel(false); //a help popup
	DecoratedPopupPanel popupInfo = new DecoratedPopupPanel(true); //a panel for an help popup
	HTML bookmarksHTML = new HTML(); //an HTML object for the display of the bookmarks
	VerticalPanel PopUpPanelContents = new VerticalPanel(); //a panel for the display of the info popup
	HTML message = new HTML(); //the info message
	boolean hasMoved; //a flag to indicate that a Tag has been dragged
	protected Label lastSelectedTag = null; //a label to save the last selected tag
	String strFilter = new String("All"); 
	
	// functional declarations
	ClientControl clientControl = ClientControl.getClientControl();
	Contact currentContact = Contact.getContact();

	ArrayList<String> strTagString = new ArrayList<String>();

	// gwt-dnd integration
	PickupDragController dragController = new PickupDragController(canvasPanel,
			false);
	
	BoundaryDropController dropController = new BoundaryDropController(
			canvasPanel, true) {
		public void onMove(DragContext context) {
			super.onMove(context);
			UIObjectConnector c = UIObjectConnector.getWrapper(context.draggable);
			if (c != null) {
				c.update();
			}
		}
	};
	

	ClickHandler clickText = new ClickHandler() {
		public void onClick(ClickEvent event) {
			Label l = (Label) event.getSource();
			
			String tagList = new String();
			for (int i = 0; i < strTagString.size(); i++) {
				tagList += strTagString.get(i) + "+";
			}
			//change it's color to reflect it's Selected Status
			l.addStyleDependentName("selected");
			if (lastSelectedTag != null) {
				lastSelectedTag.removeStyleDependentName("selected");
			}
			lastSelectedTag = l;
			
			tagList += l.getText();
			if (event.getNativeEvent().getButton() == (NativeEvent.BUTTON_LEFT) && event.getNativeEvent().getShiftKey()) {
				strTagString.add(l.getText());
				clientControl.getRelatedTags(tagList);
				smallTagsButton.setEnabled(true);
				mediumTagsButton.setEnabled(true);
				largeTagsButton.setEnabled(true);
				allTagsButton.setEnabled(true);
				allTagsButton.setDown(true);
				smallTagsButton.setDown(false);
				mediumTagsButton.setDown(false);
				largeTagsButton.setDown(false);
				bookmarksPanel.clear();
			}
			else if (event.getNativeEvent().getButton() == NativeEvent.BUTTON_LEFT) {
				// set the index of the Tag objects that has been clicked
				for (int j2 = 0; j2 < currentContact.getTagListSize(); j2++) {
					if (currentContact.getTag(j2).getTagText().equals(l.getText())) {
						currentContact.setSelectedTag(j2);
						break;
					}
				}
				// call the function to get the bookmarks
				clientControl.getBookmarks(tagList);
			} 
				
				
		}
	};

	private FrontEnd() {
	};

	private static class FrontEndHolder {
		private static final FrontEnd INSTANCE = new FrontEnd();
	}

	public static FrontEnd getFrontEnd() {
		return FrontEndHolder.INSTANCE;
	}

	//construct the GUI
	public void constructGUI() {
		okButton.addClickHandler(new ClickHandler() {
			//action on search button
			public void onClick(ClickEvent event) {
				currentContact.setName(accountTextBox.getText());
				clientControl.getTop10Tags(); //recover the top ten tags
				infoContactButton.setEnabled(true); //set info button to enabled
				pathPanel.clear(); //clear the path
			}
		});
		//action on info button
		infoContactButton.addClickHandler(new ClickHandler() {
			
			public void onClick(ClickEvent arg0) {
				//build a popup
				popupInfo.setStyleName("popupInfo");

				popupInfo.clear();
				
				Widget source = (Widget) arg0.getSource();
				int left = source.getAbsoluteLeft() + 10;
		        int top = source.getAbsoluteTop() + 10;
		        popupInfo.setPopupPosition(left, top);
				
		        HTML textPopup = new HTML();
		        String content = new String();
				String tagCombination = new String();
				
				//build a message of the contact is not null
				if (currentContact != null) {
				
					for (int i = 0; i < strTagString.size(); i++) {
						tagCombination += strTagString.get(i) + "-";
					}
					if (currentContact.getSelectedTag() != -1)
						tagCombination += currentContact.getTag(currentContact.getSelectedTag()).getTagText();
					
					String numberLinks = "0";
					if (currentContact.getSelectedTag() > 0) {
						numberLinks = Integer.toString(currentContact.getTag(currentContact.getSelectedTag()).getNrLinks());
					}
					
					//html code for the content
					content = "<p><b>Account Name : </b>" + currentContact.getName() + "</p>" +  
								"<p><b>Tag Combination : </b>" + tagCombination + "</p>" + 
								"<p><b>Related Tags : </b>" + Integer.toString(currentContact.getTagListSize()) + "</p>" +
								"<p><b>Bookmarks : </b>" + numberLinks + "</p>";
					
					textPopup.setHTML(content);
					
				}
				
				//close the popup with a click
				textPopup.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent arg0) {
						popupInfo.hide();
						
					}
					
				});
				
				popupInfo.add(textPopup);
				
				popupInfo.show();
				
				
			}
		});
		
		//by default the info button is disabled
		infoContactButton.setEnabled(false);
		//build the banner panel
		bannerPanel.addStyleName("bannerPanel");
		banner.setUrl("Images/logo.jpg");
		bannerPanel.add(banner);
		
		//build the canvas
		canvasPanel.addStyleName("canvas");
		
		//build the account text box
		accountTextBox.setSize("230", "25");
		accountTextBox.addKeyUpHandler(new KeyUpHandler() {
			//enter launch the search
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					currentContact.setName(accountTextBox.getText());
					clientControl.getTop10Tags();
					infoContactButton.setEnabled(true);
				}
			}
		});
		accountTextBox.setText("enter account name here");
		accountTextBox.setFocus(true);
		accountTextBox.setSelectionRange(0, accountTextBox.getText().length());
		
		//build the option panel
		optionsPanelFirstLine.add(accountTextBox);
		optionsPanelFirstLine.add(okButton);
		optionsPanelFirstLine.add(infoContactButton);
		optionsPanelFirstLine.addStyleName("searchbar");
		optionsPanelSecondLine.setSpacing(5);
		optionsPanelSecondLine.add(clearButton);
		optionsPanelSecondLine.add(helpButton);
		optionsPanelSecondLine.add(smallTagsButton);
		optionsPanelSecondLine.add(mediumTagsButton);
		optionsPanelSecondLine.add(largeTagsButton);
		optionsPanelSecondLine.add(allTagsButton);
		//handlers for the buttons
		ClickHandler toogleClick = new ClickHandler() {
			public void onClick(ClickEvent event) {
				ToggleButton temp = (ToggleButton)event.getSource();
				//case of All button pressed
				if (temp.getText().equals("A")) {
					if (!temp.isDown()) {
						//by default the all tags button is down
						allTagsButton.setDown(true);
					}
					else {
						smallTagsButton.setDown(false);
						mediumTagsButton.setDown(false);
						largeTagsButton.setDown(false);
						strFilter = "All";
					}
				}
				//case of Small button pressed
				if (temp.getHTML().contains("lettre_s")) {
					if (!temp.isDown()) {
						smallTagsButton.setDown(true);
					}
					else {
						allTagsButton.setDown(false);
						mediumTagsButton.setDown(false);
						largeTagsButton.setDown(false);
						strFilter = "Small";
					}
				}
				
				//case of Medium button pressed
				if (temp.getHTML().contains("lettre_m")) {
					if (!temp.isDown()) {
						mediumTagsButton.setDown(true);
					}
					else {
						smallTagsButton.setDown(false);
						allTagsButton.setDown(false);
						largeTagsButton.setDown(false);
						strFilter = "Medium";
					}
				}
				//case of Large button pressed
				if (temp.getHTML().contains("lettre_l")) {
					if (!temp.isDown()) {
						largeTagsButton.setDown(true);
					}
					else {
						smallTagsButton.setDown(false);
						mediumTagsButton.setDown(false);
						allTagsButton.setDown(false);
						strFilter = "Large";
					}
				}
				//refresh the tags on screen
				displayRelatedTag();
			}
		};
		
		smallTagsButton.addClickHandler(toogleClick);
		mediumTagsButton.addClickHandler(toogleClick);
		largeTagsButton.addClickHandler(toogleClick);
		allTagsButton.addClickHandler(toogleClick);
		
		//set the icon for the clear and help buttons
		clearButton.setHTML("<IMG SRC=\"Images/clear.png\" ALIGN=\"absmiddle\">");
		helpButton.setHTML("<IMG SRC=\"Images/help.png\" ALIGN=\"absmiddle\">");
		//add an action for the clear and help buttons
		clearButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				cleanUp();
				
			}
		});
		helpButton.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {

				popupHelp.setStyleName("w-popup");
				popupHelp.setTitle("Help");
				PopUpPanelContents = new VerticalPanel();

				Button button = new Button("Close");
				final SimplePanel holder = new SimplePanel();
				holder.add(button);
				holder.setStyleName("help-popup-footer");

				button.addClickHandler(new ClickHandler() {

					public void onClick(ClickEvent event) {

						popupHelp.hide();
						popupHelp.setVisible(false);
						
						try{
						holder.clear();
						scrollPopup.clear();
						PopUpPanelContents.clear();
						popupHelp.clear();
						}
						catch(Exception e)
						{
							System.out.println("Can't launch HELP again!");
						}
						
					}
				});

				Frame frame = new Frame("HelpFrames/general_frame.html");
				frame.setWidth("370px");
				frame.setHeight("320px");
				scrollPopup.add(frame);

				scrollPopup.setSize("400px", "350px");
				PopUpPanelContents.add(scrollPopup);
				PopUpPanelContents.add(holder);
				popupHelp.setWidget(PopUpPanelContents);
				popupHelp.center();
				popupHelp.setVisible(true);
			}
		});
		
		//set icons for the buttons
		smallTagsButton.setHTML("<IMG SRC=\"Images/lettre_s.gif\" ALIGN=\"absmiddle\">");
		mediumTagsButton.setHTML("<IMG SRC=\"Images/lettre_m.gif\" ALIGN=\"absmiddle\">");
		largeTagsButton.setHTML("<IMG SRC=\"Images/lettre_l.gif\" ALIGN=\"absmiddle\">");
		allTagsButton.setHTML("<IMG SRC=\"Images/lettre_a.gif\" ALIGN=\"absmiddle\">");
		
		//by default the tags buttons are disabled
		smallTagsButton.setEnabled(false);
		mediumTagsButton.setEnabled(false);
		largeTagsButton.setEnabled(false);
		allTagsButton.setEnabled(false);
		
		optionsPanel.add(optionsPanelFirstLine);
		optionsPanel.add(optionsPanelSecondLine);

		optionsPanel.addStyleName("options");

		//put a title
		Label titleOptionsLabel = new Label("Options");
		titleOptionsPanel.add(titleOptionsLabel);
		titleOptionsPanel.addStyleName("optionsTitle");
		
		//build the path panel
		scrollPathPanel.addStyleName("path");
		scrollPathPanel.add(pathPanel);
		
		Label titlePathLabel = new Label("Path");
		titlePathPanel.add(titlePathLabel);
		titlePathPanel.addStyleName("pathTitle");

		//build the bookmarks panel
		Label bookmarksTitleLabel = new Label("Bookmarks");
		titleBookmarksPanel.add(bookmarksTitleLabel);
		titleBookmarksPanel.setStyleName("bookmarksTitle");
		
		bookmarksPanel.add(bookmarksHTML);
		// bookmarksPanel.addStyleName("bookmarks");
		bookmarksScroll.add(bookmarksPanel);
		bookmarksScroll.setTitle("Bookmarks");
		bookmarksScroll.addStyleName("bookmarks");

		//add the panels in a bigger one on the right side of the GUI
		rightFramePanel.add(titleOptionsPanel);
		rightFramePanel.add(optionsPanel);
		rightFramePanel.add(titlePathPanel);
		rightFramePanel.add(scrollPathPanel);
		rightFramePanel.add(titleBookmarksPanel);
		rightFramePanel.add(bookmarksScroll);
		rightFramePanel.addStyleName("rightFrame");

		//add the three main panels in the body
		bodyPanel.add(bannerPanel);
		bodyPanel.add(canvasPanel);
		bodyPanel.add(rightFramePanel);
		bodyPanel.addStyleName("bodyPanel");

		//create the root panel
		allPanel.add(bodyPanel);
		allPanel.addStyleName("allPanel");

		RootPanel.get("visuallinks").add(allPanel);

		dragController.registerDropController(dropController);
	}

	public void refreshBookmarks() {
		//clear the existing bookmarks
		bookmarksPanel.clear();
		int intSelectedTag = currentContact.getSelectedTag();
		//if invalid number then exit function
		if (intSelectedTag == -1) return;
		Tag currentTag = currentContact.getTag(intSelectedTag);
		Links link;
		//build the bookmarks panel
		for (int i = 0; i < currentTag.getEffectiveNrLinks(); i++) {
			//each bookmark in a disclosure panel
			DisclosurePanel disclosurePanel = new DisclosurePanel();
			HTML bookmarksHTML = new HTML();
			HTML contentHTML = new HTML();
			String bookmarks = new String();
			link = currentTag.getLink(i);
			String linkString = link.getLink();

			//build the content of a disclosure panel
			bookmarks += "<p><a target=\"_blank\" href=\""
					+ linkString.substring(1, linkString.length() - 1) + "\">"
					+ link.getDescription() + "</a></p>\n";
			bookmarksHTML.setHTML(bookmarks);
			disclosurePanel.setHeader(bookmarksHTML);
			contentHTML.setHTML(bookmarks
					+ "<p>"
					+ link.getTitle()
							.substring(1, link.getTitle().length() - 1)
					+ "</p>");
			disclosurePanel.setContent(contentHTML);
			disclosurePanel.addStyleName("disclosurePanel");
			bookmarksPanel.add(disclosurePanel);
			// bookmarksPanel.setBorderWidth(1);

		}
	}

	@SuppressWarnings("deprecation")
	public void refreshPath() {
		pathPanel.clear();
		for (int i = 0; i < strTagString.size(); i++) {
			Hyperlink tag = new Hyperlink(strTagString.get(i), "<a>");
			tag.addClickHandler(new ClickHandler() {
				public void onClick(ClickEvent event) {
					Hyperlink h = (Hyperlink) event.getSource();
					String urlPath = new String();
					ArrayList<String> tempTagList = new ArrayList<String>();
					for (int j = 0; j < strTagString.size(); j++) {
						if (!strTagString.get(j).equals(h.getText())) {
							urlPath += strTagString.get(j) + "+";
							tempTagList.add(strTagString.get(j));
						}
						else
							break;
					}
					urlPath += h.getText();
					tempTagList.add(h.getText());
					strTagString.clear();
					strTagString.addAll(tempTagList);
					strFilter = "All";
					allTagsButton.setDown(true);
					smallTagsButton.setDown(false);
					mediumTagsButton.setDown(false);
					largeTagsButton.setDown(false);
					clientControl.getRelatedTags(urlPath);
				}
			});
			Image arrow = new Image("Images/arrow_right.png");
			pathPanel.add(tag);
			if (i < strTagString.size() - 1)
				pathPanel.add(arrow);
		}
	}

	public void displayContact() {
		// display Contact Label
		int contactX = 300;
		int contactY = 300;
		Label contactlabel = new Label(currentContact.getName());
		contactlabel.setStyleName("contact-style");
		canvasPanel.clear();
		canvasPanel.add(contactlabel, contactX, contactY);
		// create tags and connectors
		int intTagListSize = currentContact.getTagListSize();
		for (int i = 0; i < intTagListSize; i++) {
			Vtext tempTagLabel = new Vtext(currentContact.getTag(i).getTagText());
			tempTagLabel.setStylePrimaryName("tag-style");
			int nrLinks = currentContact.getTag(i).getNrLinks();
			if (nrLinks <= currentContact.getLow()) {
				tempTagLabel.addStyleDependentName("12Font");
			}
			else if (nrLinks <= currentContact.getMedium()) {
				tempTagLabel.addStyleDependentName("20Font");
			}
			else {
				tempTagLabel.addStyleDependentName("24Font");
			}
			// position the tags in a circle around the contact
			int x = (int) (300 + 200 * Math.cos(i * (360/intTagListSize) * Math.PI / 180));
			int y = (int) (300 + 200 * Math.sin(i * (360/intTagListSize) * Math.PI / 180));
			// add a random deviation to the positions
			x -= Random.nextInt(20) - 10;
			y -= Random.nextInt(20) - 10;
			tempTagLabel.setWordWrap(true);
			tempTagLabel.addClickHandler(clickText);
			canvasPanel.add(tempTagLabel, x, y);
			dragController.makeDraggable(tempTagLabel);
			Connector c1 = UIObjectConnector.wrap(tempTagLabel);
			Connector c2 = UIObjectConnector.wrap(contactlabel);
			Connection line = new StraightTwoEndedConnection(c1, c2);
			line.appendTo(canvasPanel);

			canvasPanel.insert(canvasPanel.getWidget(canvasPanel.getWidgetIndex(tempTagLabel) + 1), 1);
		}
	}

	public void displayRelatedTag() {
		// display CurrentTag Label
		int centerTagX = 300;
		int centerTagY = 300;
		Label centerLabel = new Label(strTagString.get(strTagString.size() - 1));
		centerLabel.setStyleName("contact-style");
		canvasPanel.clear();
		canvasPanel.add(centerLabel, centerTagX, centerTagY);
		int intTagListSize = currentContact.getTagListSize();
		//calculate the number of tags to show on screen
		int nrLarge, nrMedium, nrSmall, nrTags = 0;
		if (strFilter.equals("All")) {
			nrTags = intTagListSize;
		}
		else if (strFilter.equals("Small")) {
			nrSmall = 0;
			for (int i = 0; i < intTagListSize; i++) {
				if (currentContact.getTag(i).getNrLinks() < currentContact.getLow()) {
					nrSmall++;
				}
			}
			nrTags = nrSmall;
		}
		else if (strFilter.equals("Medium")) {
			nrMedium = 0;
			for (int i = 0; i < intTagListSize; i++) {
				if (currentContact.getTag(i).getNrLinks() < currentContact.getMedium() && 
					currentContact.getTag(i).getNrLinks() >= currentContact.getLow()) {
					nrMedium++;
				}
			}
			nrTags = nrMedium;
		}
		else if (strFilter.equals("Large")) {
			nrLarge = 0;
			for (int i = 0; i < intTagListSize; i++) {
				if (currentContact.getTag(i).getNrLinks() <= currentContact.getHigh() &&
					currentContact.getTag(i).getNrLinks() >= currentContact.getMedium()) {
					nrLarge++;
				}
			}
			nrTags = nrLarge;
		}
		// create tags and connectors
		for (int i = 0; i < intTagListSize; i++) {
			Vtext tempTagLabel = new Vtext(currentContact.getTag(i).getTagText());
			tempTagLabel.setStylePrimaryName("tag-style");
			int nrLinks = currentContact.getTag(i).getNrLinks();
			
			if (nrLinks <= currentContact.getLow()) {
				tempTagLabel.addStyleDependentName("12Font");
				if (!strFilter.equals("Small") && !strFilter.equals("All"))
					continue;
			}
			else if (nrLinks <= currentContact.getMedium()) {
				tempTagLabel.addStyleDependentName("20Font");
				if (!strFilter.equals("Medium") && !strFilter.equals("All"))
					continue;
			}
			else {
				tempTagLabel.addStyleDependentName("24Font");
				if (!strFilter.equals("Large") && !strFilter.equals("All"))
					continue;
			}
			// position the tags in a circle around the contact
			
			int x = (int) (300 + 200 * Math.cos(Math.toRadians(i * (360/nrTags))));
			int y = (int) (300 + 200 * Math.sin(Math.toRadians(i * (360/nrTags))));

			//System.out.println("x:" + String.valueOf(x) + "y:" + String.valueOf(y));
			// add a random deviation to the positions
			x -= Random.nextInt(20) - 10;
			y -= Random.nextInt(20) - 10;
			tempTagLabel.setWordWrap(true);
			tempTagLabel.addClickHandler(clickText);
			tempTagLabel.sinkEvents(Event.ONDBLCLICK);
			canvasPanel.add(tempTagLabel, x, y);
			dragController.makeDraggable(tempTagLabel);
			Connector c1 = UIObjectConnector.wrap(tempTagLabel);
			Connector c2 = UIObjectConnector.wrap(centerLabel);
			Connection line = new StraightTwoEndedConnection(c1, c2);
			line.appendTo(canvasPanel);
			canvasPanel.insert(canvasPanel.getWidget(canvasPanel.getWidgetIndex(tempTagLabel) + 1), 1);
		}
	}
	
	public void cleanUp() {
		canvasPanel.clear();
		pathPanel.clear();
		bookmarksPanel.clear();
		strTagString = new ArrayList<String>();
		currentContact.clear();
		currentContact.setName("");
		accountTextBox.setText("");
		infoContactButton.setEnabled(false);
		smallTagsButton.setEnabled(false);
		mediumTagsButton.setEnabled(false);
		largeTagsButton.setEnabled(false);
		allTagsButton.setEnabled(false);
		allTagsButton.setDown(false);
		smallTagsButton.setDown(false);
		mediumTagsButton.setDown(false);
		largeTagsButton.setDown(false);
	}
}
